package com.demo2do.fms.admin.web.controller.report;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.entity.page.Page;
import com.demo2do.core.web.interceptor.MenuSetting;
import com.demo2do.core.web.utils.WebUtils;
import com.demo2do.fms.admin.entity.export.report.TransportToolSheet;
import com.demo2do.fms.admin.entity.report.report.BusinessHistoryReportSearch;
import com.demo2do.fms.admin.entity.report.report.ParkingResourceReportSearch;
import com.demo2do.fms.admin.entity.report.report.StaffReportSearch;
import com.demo2do.fms.admin.entity.search.report.TransportToolSearch;
import com.demo2do.fms.admin.service.report.SupplierReportService;
import com.demo2do.fms.admin.utils.FormatUtils;
import com.demo2do.fms.admin.web.view.ExcelTemplateView;

/**
 * 供方报表管理
 *
 * @author Ark
 *
 */
@Controller
@RequestMapping("/supplier-reports")
@MenuSetting("menu-report")
public class SupplierReportController {
    
    private static final Logger logger = LoggerFactory.getLogger(SupplierReportController.class);
    
    @Autowired
    private SupplierReportService supplierReportService;

    /**
     * 过往业务信息查询page
     * 
     * @return
     */
    @RequestMapping(value = "/business-history-search", method = RequestMethod.GET)
    public String businessHistorySearch() {
        return "report/supplier-report/business-history-search";
    }
    
    /**
     * 过往业务信息查询list
     * 
     * @param search
     * @param request
     * @param page
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET, params = "business-history")
    @MenuSetting("submenu-business-history-statistic")
    public ModelAndView onBusinessHistorySearch(BusinessHistoryReportSearch search, HttpServletRequest request, Page page) {
        
        ModelAndView modelAndView = new ModelAndView("report/supplier-report/business-history-list");
        
        if (CollectionUtils.isNotEmpty(search.getStatus())) {
            search.addStatus(search.getStatus());
        }
        
        int totals = supplierReportService.count(search);
        if (totals > 0) {
            
            List<Map<String, Object>> results = supplierReportService.search(search, page.with(totals));
            modelAndView.addObject("results", results);
            
            modelAndView.addObject("page", page);
            modelAndView.addObject("queryString", WebUtils.extractQueryString(request, "page"));
            
        }
        
        return modelAndView;
        
    }
    
    /**
     * 过往业务信息导出
     * 
     * @param search
     * @param page
     * @return
     */
    @RequestMapping(value = "/business-history-export", method = RequestMethod.GET)
    public ModelAndView onBusinessHistoryExport(BusinessHistoryReportSearch search, Page page) {
        
        ModelAndView modelAndView = new ModelAndView(new ExcelTemplateView("report/business-history-export.xlsx"));
        
        if (CollectionUtils.isNotEmpty(search.getStatus())) {
            search.addStatus(search.getStatus());
        }
        
        // 1. list driver distance
        List<Map<String, Object>> results = supplierReportService.search(search, page);
        modelAndView.addObject("results", results);
        
        // 2. format util
        FormatUtils fmt = new FormatUtils();
        modelAndView.addObject("fmt", fmt);
        
        return modelAndView;
        
    }
    
    /**
     * 场地资源信息查询page
     * 
     * @return
     */
    @RequestMapping(value = "/parking-resource-search", method = RequestMethod.GET)
    public String parkingResourceSearch() {
        return "report/supplier-report/parking-resource-search";
    }
    
    /**
     * 场地资源信息查询list
     * 
     * @param search
     * @param request
     * @param page
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET, params = "parking-resource")
    @MenuSetting("submenu-parking-resource-statistic")
    public ModelAndView onParkingResourceSearch(ParkingResourceReportSearch search, HttpServletRequest request, Page page) {
        
        ModelAndView modelAndView = new ModelAndView("report/supplier-report/parking-resource-list");
        
        if (CollectionUtils.isNotEmpty(search.getStatus())) {
            search.addStatus(search.getStatus());
        }
        
        int totals = supplierReportService.count(search);
        if (totals > 0) {
            
            List<Map<String, Object>> results = supplierReportService.search(search, page.with(totals));
            modelAndView.addObject("results", results);
            
            modelAndView.addObject("page", page);
            modelAndView.addObject("queryString", WebUtils.extractQueryString(request, "page"));
            
        }
        
        return modelAndView;
        
    }
    
    /**
     * 场地资源信息导出
     * 
     * @param search
     * @param page
     * @return
     */
    @RequestMapping(value = "/parking-resource-export", method = RequestMethod.GET)
    public ModelAndView onParkingResourceExport(ParkingResourceReportSearch search, Page page) {
        
        ModelAndView modelAndView = new ModelAndView(new ExcelTemplateView("report/parking-resource-export.xlsx"));
        
        if (CollectionUtils.isNotEmpty(search.getStatus())) {
            search.addStatus(search.getStatus());
        }
        
        // 1. list driver distance
        List<Map<String, Object>> results = supplierReportService.search(search, page);
        modelAndView.addObject("results", results);
        
        // 2. format util
        FormatUtils fmt = new FormatUtils();
        modelAndView.addObject("fmt", fmt);
        
        return modelAndView;
        
    }
    
    /**
     * 关键岗位信息统计page
     * 
     * @return
     */
    @RequestMapping(value = "/staff-search", method = RequestMethod.GET)
    public String staffSearch() {
        return "report/supplier-report/staff-search";
    }
    
    /**
     * 关键岗位信息统计list
     * 
     * @param search
     * @param request
     * @param page
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET, params = "staff")
    @MenuSetting("submenu-staff-statistic")
    public ModelAndView onStaffSearch(StaffReportSearch search, HttpServletRequest request, Page page) {
        
        ModelAndView modelAndView = new ModelAndView("report/supplier-report/staff-list");
        
        if (CollectionUtils.isNotEmpty(search.getStatus())) {
            search.addStatus(search.getStatus());
        }
        
        int totals = supplierReportService.count(search);
        if (totals > 0) {
            
            List<Map<String, Object>> results = supplierReportService.search(search, page.with(totals));
            modelAndView.addObject("results", results);
            
            modelAndView.addObject("page", page);
            modelAndView.addObject("queryString", WebUtils.extractQueryString(request, "page"));
            
        }
        
        return modelAndView;
        
    }
    
    /**
     * 关键岗位信息导出
     * 
     * @param search
     * @param page
     * @return
     */
    @RequestMapping(value = "/staff-export", method = RequestMethod.GET)
    public ModelAndView onStaffExport(StaffReportSearch search, Page page) {
        
        ModelAndView modelAndView = new ModelAndView(new ExcelTemplateView("report/staff-export.xlsx"));
        
        if (CollectionUtils.isNotEmpty(search.getStatus())) {
            search.addStatus(search.getStatus());
        }
        
        // 1. list driver distance
        List<Map<String, Object>> results = supplierReportService.search(search, page);
        modelAndView.addObject("results", results);
        
        // 2. format util
        FormatUtils fmt = new FormatUtils();
        modelAndView.addObject("fmt", fmt);
        
        return modelAndView;
        
    }
    
    /**
     * 运输工具汇总 - panel
     * 
     * @return
     */
    @RequestMapping(value = "/transport-tool-search", method = RequestMethod.GET)
    @MenuSetting("submenu-transport-tool")
    public ModelAndView transportToolSearch() {
        ModelAndView modelAndView = new ModelAndView("report/supplier-report/transport-tool-search");
        return modelAndView;
    }
    
    /**
     * 运输工具汇总
     * 
     * @param search
     * @param request
     * @param page
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET, params = "transport-tool")
    @MenuSetting("submenu-transport-tool")
    public ModelAndView onTransportToolSearch(TransportToolSearch search, HttpServletRequest request, Page page) {
        
        ModelAndView modelAndView = new ModelAndView("report/supplier-report/transport-tool-list");
        
        int totals = supplierReportService.count(search);
        if (totals > 0) {
            
            List<TransportToolSheet> transportTools = supplierReportService.search(search, page.with(totals));
            modelAndView.addObject("transportTools", transportTools);
            
            modelAndView.addObject("page", page);
            modelAndView.addObject("queryString", WebUtils.extractQueryString(request, "page"));
            
        }
        
        return modelAndView;
        
    }
    
    /**
     * 运输工具汇总 - 导出
     * 
     * @param search
     * @return
     */
    @RequestMapping(value = "/transport-tool-export", method = RequestMethod.GET)
    public ModelAndView transportToolExport(TransportToolSearch search) {
        
        ModelAndView modelAndView = new ModelAndView(new ExcelTemplateView("report/transport-tool-export.xlsx"));
        
        List<TransportToolSheet> tools = supplierReportService.export(search);
        modelAndView.addObject("tools", tools);
        
        logger.warn("#transportToolExport() - export size[{}] end...", tools.size());
        
        return modelAndView;
        
    }
    
}
